<?php

class ReviewsController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Reviews'),
        ));
    }

    public function actioncreate() {
        $model = new Reviews;
        if (isset($_POST['text'])) {
            $userid = getLoginUserId();
            if (!empty($userid)) {
                if (isset($_POST['classId'])) {
                    $model->setAttribute('classes_id', $_POST['classId']);
                    $classCourseUser = Classes::model()->findByPk($_POST['classId']);
                    $classCourseUser = ($classCourseUser->users_id != '') ? $classCourseUser->users_id : '';
                    $reviewsUser = $model->findByAttributes(array('classes_id' => $_POST['classId'], 'users_id' => $userid));
                } if (isset($_POST['courseId'])) {
                    $model->setAttribute('courses_id', $_POST['courseId']);
                    $classCourseUser = Courses::model()->findByPk($_POST['courseId']);
                    $classCourseUser = ($classCourseUser->users_id != '') ? $classCourseUser->users_id : '';
                    $reviewsUser = $model->findByAttributes(array('courses_id' => $_POST['courseId'], 'users_id' => $userid));
                }

                if ($reviewsUser <> "" && $classCourseUser != $userid && $reviewsUser->users_id != $userid) {
                    $model->setAttribute('reviews_text', $_POST['text']);
                    $model->setAttribute('users_id', $userid);
                    $model->setAttribute('create_time', SkillConfig::CreateDateTime());
                    if ($model->save(FALSE)) {
                        echo Message::REVIEWS_SUCCESS_MSG;
                    }
                } elseif ($reviewsUser == "" && $classCourseUser != $userid) {
                    $model->setAttribute('reviews_text', $_POST['text']);
                    $model->setAttribute('users_id', $userid);
                    $model->setAttribute('create_time', SkillConfig::CreateDateTime());
                    if ($model->save(FALSE)) {
                        echo Message::REVIEWS_SUCCESS_MSG;
                    }
                }

                if ($reviewsUser <> "" && ($reviewsUser->users_id == $userid)) {
                    echo 'againsameuser';
                }

                if ($classCourseUser == $userid) {
                    echo 'fail';
                }
            } else {
                echo 'redirect';
            }
        }
    }

    public function actiontestimonials() {
        $this->layout = 'detailspage';
        $dataProvider = new CActiveDataProvider('Reviews', array(
                    'criteria' => array(
                        'condition' => 'status=' . SkillConfig::STATUS_ACTIVE,
                        'order' => 'create_time DESC',
                    ),
                    'pagination' => array(
                        'pageSize' => 10,
                    ),
                ));
        $this->render('_viewAllReview', array('dataProvider' => $dataProvider));
    }

}
